<?php

/**
 * SupplierController is the default controller for this application
 */
class SupplierController extends ControllerBase {

	/**
	 * Admin Supplier Data Service Object
	 * @var SupplierDataService object
	 */
	private $SupplierDataService;

	/**
	 * SupplierController Constructor
	 */
	public function __construct() {
		parent::__construct();

		// Set Supplier name
		$this->setPageName('admin_supplier');

		// Init Authentication class
		$authentication = new Authentication();

		// Only administrator can right
		if (!($authentication->isAdministrator () == true || $authentication->getUserType() == ROLE_DEALMANAGER)) {
				
			// Go to login Supplier
			$url = Zend_Registry::get('config')->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect($url);
		}

		// Get status list of Supplier category
//		$this->smarty->assign('statusList', SupplierEntity::getStatusList());

		// New Supplier Data Service
		$this->SupplierDataService = new SupplierDataService();
	}

	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing Supplier of the site. Return list of all Supplier category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View Supplier_category list
		$this->listAction();
	}

	/**
	 * Dispplay List of Supplier_category
	 * landing Supplier of the site. Return list of all Supplier_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
	
		$arrElement = array();
		//$arrElement['status'] = SupplierEntity::ACTIVE;

		$numRecordPerPage = ITEM_PER_PAGE;

		// Pager
		$start = $this->getQuery('start', 0);
		if ($start == '' || $start < 0){
			$start = 0;
		}

		// Get Totals Templates
		$totals = $this->SupplierDataService->getTotals($arrElement);
		
		$itemList = $this->SupplierDataService->getItems($arrElement, NULL, $start, $numRecordPerPage);

		if ($start > $totals){ $start = 0; }
		$link = Zend_Registry::get('config')->APPLICATION_URL. "/Admin/Supplier/Index";
		$pageLink = '';
		if($totals > $numRecordPerPage){
			$pageUtils = new PagerUtils($start, $totals, $numRecordPerPage, 10, $link);
			$pageLink = $pageUtils->writePagesLinks(0, 'start');
			$pageLink .= "<br><span class=\"paing_text_nolink\">".$pageUtils->writePagesCounter()."</span>";
		}

		$this->smarty->assign('SupplierEntities', $itemList);
		$this->smarty->assign('this', $this);
		$this->smarty->assign('pageLink', $pageLink);
		
		// create session
		$this->createActionKey();
		$this->smarty->assign('SMARTY_CONTENT_PATH', 'Admin/Supplier/index.tpl');
		$this->smarty->display(APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl');
	}

	/**
	 * Add action is trigger when admin want to add the Supplier_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function addAction() {

		$isError = false;
		$msg = "";

		// Get action method and check it
		$actionForm = $this->getPost('submit', '');

		if (
		!empty($actionForm)
		) {

			// Get supplier name
			$supplier_name = $this->getPost('supplier_name', "");
			$supplier_name = trim($supplier_name);
			// get supplier_address
			$supplier_address = $this->getPost('supplier_address', "");
				
			
		
			// Upload logo logo
			$logo= '';
			try{
				$logo = CommonUtils::uploadPictureEx('logo');
			}catch (Exception $ex){

				// Ignore this case
				$resultMessage = 'Upload logo fail.';
			}
				
			
				
			//	get phone number
			$phone = $this->getPost('phone', "");
				
			// Get website field
			$website = $this->getPost('website', "");
				
			// check
			if (!empty($supplier_name)
			&& !empty($supplier_address)
			&& !empty($phone)
			&& !empty($website)
			) {
				try {
						
					// Init Authentication class
					$authentication = new Authentication();
					// Save data into database
					$supplierEntity = $this->SupplierDataService->beforeCreate();
					$supplierEntity->supplier_name = $supplier_name;
					$supplierEntity->supplier_address = $supplier_address;
					$supplierEntity->logo = $logo;
					$supplierEntity->phone = $phone;
					$supplierEntity->website = $website;
						
					$supplierId = $this->SupplierDataService->create($supplierEntity);
						
					// Redirect to edit Supplier
					$url = Zend_Registry::get('config')->APPLICATION_URL . '/Admin/Supplier/';
					CommonUtils::redirect($url);
					exit;
				}
				catch (Exception $ex) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode();
						
					echo $ex->getMessage();
						
				}
			}
			else {
				$isError = true;

				$msg = 'Please fill in all required fields';
			}
		}

		// Create new token key
		$this->createActionKey();
		$this->smarty->assign('msg', $msg);
		$this->smarty->assign('isError', $isError);
		$this->smarty->assign('supplier_name', isset($supplier_name) ? $supplier_name : "");
		$this->smarty->assign('supplier_address', isset($supplier_address) ? $supplier_address : "");
		$this->smarty->assign('phone', isset($phone) ? $phone : "");
		$this->smarty->assign('website', isset($website) ? $website : "");
		$this->smarty->assign('SMARTY_CONTENT_PATH', 'Admin/Supplier/add.tpl');
		$this->smarty->display(APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl');
	}

	/**
	 * Edit action is trigger when admin want to edit the Supplier_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		// Error flag
		$isError = false;
		$msg = "";
		
		// Get id of Supplier
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of Supplier does not exist, go on to view list Supplier
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->listAction ();
			return false;
		}
		
		$id = intval ( $id );
			
		// Get data
		$SupplierEntity = $this->SupplierDataService->getById($id );
		// check data
		if (empty ( $SupplierEntity )) {
			$this->listAction ();
			return false;
		}
		// Get action method and check it
		$actionForm = $this->getPost ( 'submit', '' );
		
		// submit
		if (! empty ( $actionForm )) {
			// Get Supplier name field
			$Supplier_name = $this->getPost ( 'supplier_address', "" );
			$Supplier_name = trim ( $Supplier_name );
			$SupplierEntity->supplier_name = $Supplier_name;
			
			// Get supplier_address field
			$Supplier_address = $this->getPost('supplier_address',"");
			$Supplier_name= trim($Supplier_name);
			$SupplierEntity->supplier_address = $Supplier_address;
			
			// Get phone field
			$phone = $this->getPost('phone',"");
			$phone = trim($phone);
			$SupplierEntity->phone = $phone;
			
			// get website
			$website = $this->getPost('website',"");
			$website = trim($website);
			$SupplierEntity->website = $website;
			
			// Upload logo logo
			
			try{
				$logo = CommonUtils::uploadPictureEx('logo');
			}catch (Exception $ex){

				// Ignore this case
				$resultMessage = 'Upload logo fail.';
			}
			$SupplierEntity->logo=$logo;
			// check
			if (! empty ( $Supplier_name )
				&& ! empty ( $Supplier_address )
				&& ! empty ( $phone )
				&& ! empty ( $website )) {
				try {
					$res = $this->SupplierDataService->update ( $SupplierEntity );
					//var_dump($res);
					$isError = false;
					
					// Show a message
					$msg = 'The record has been updated successfully';
					// Redirect to edit Supplier
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Supplier/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} 
			
			else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
			
		}
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign('id',$SupplierEntity->id);
		$this->smarty->assign ( 'supplier_name', $SupplierEntity->supplier_name );
		$this->smarty->assign ( 'supplier_address', $SupplierEntity->supplier_address);
		$this->smarty->assign ( 'phone', $SupplierEntity->phone);
		$this->smarty->assign ( 'website', $SupplierEntity->website);
		$this->smarty->assign ( 'logo', $SupplierEntity->logo);
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Supplier/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}

	/**S
	 * Delete action is trigger when administrator want to delete a Supplier_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get Id of Supplier
		$id = $this->getPost('id', 0);

		if (
		$this->validateActionKey()
		&& !empty($id)
		&& is_numeric($id)
		) {
				
			$id = intval($id);
			// Delete Supplier
			$res = $this->$supplierEntity->del($id);
		}
		$this->listAction();
	}


}
